<?php
/**
 * 角色修改类
 * User: Administrator
 * Date: 2018\8\31 0031
 * Time: 17:08
 */

namespace app\Lib\Model\Role\RoleManage;


use app\Lib\Exception\BlException;
use app\Lib\Exception\SysException;
use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\InterfaceIn\Role\RoleManage\SaveRoleHandle;

class SaveRole extends RoleOperatingBase implements SaveRoleHandle
{
    /** @var RoleHandle $roleObj */
    protected $roleObj;
    /**
     * 初始化
     * SaveRoleHandle constructor.
     * @param RoleHandle $roleObj
     */
    public function __construct(RoleHandle $roleObj)
    {
        $this->roleObj = $roleObj;
    }

    /**
     * 执行代码
     * @return bool
     * @throws BlException
     * @throws SysException
     */
    public function run()
    {
        $model        = $this->getRoleDb();
        $roleValidate = $this->getRoleValidate();
        $data         =  $this->getHandleArray();
        $data[$model->_save_time] = time();
        $data[$model->_save_uid]  = $this->user->getUid();
        $data[$model->_id]        = $this->roleObj->getRoleId();
        if(!$roleValidate->scene('save')->check($data)){
            throw new BlException($roleValidate->getError());
        }
        if(!$model->update($data)){
            throw new SysException('修改角色失败');
        }
        return true;
    }

}